package io.renren.modules.lables.controller;

import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.R;
import io.renren.modules.lables.service.LabelService;
import io.renren.modules.meta.entity.EventEntity;
import io.renren.modules.meta.entity.SubjectEntity;
import io.renren.modules.meta.service.EventEntityService;
import io.renren.modules.meta.service.SubjectService;
import io.renren.modules.subject.entity.BasePersons;
import io.renren.modules.subject.entity.BaseSubjectExtend;
import io.renren.modules.subject.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@Slf4j
public class LablesController {
    @Autowired
    private LabelService labelService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private EventEntityService eventEntityService;
    @Autowired
    private CriminalStrikePersonsService criminalStrikePersonsService;
    @Autowired
    private FraudulentPersonsService fraudulentPersonsService;
    @Autowired
    private PenaltyPersonsService penaltyPersonsService;
    @Autowired
    private CarService carService;
    @Autowired
    private PhoneService phoneService;

    @RequestMapping("/userprofile/{subjectId}/{userId}")
    public R userprofile(@PathVariable("subjectId") Long subjectId, @PathVariable("userId") Long userId) {
        SubjectEntity subjectEntity = subjectService.getById(subjectId);
        String subjectName = subjectEntity.getSubjectName();
        ArrayList<String> subjectTags = labelService.getSubjectTags(subjectName, subjectId);
        HashMap<String, List<BasePersons>> persons = new HashMap<>();
        for (String subjectTag : subjectTags) {
            HashMap<String, List<String>> users = labelService.circleSelect(new String[]{subjectTag});
            List<BasePersons> personList = new ArrayList<>();
            for (String subject : users.keySet()) {
                List<String> ids = users.get(subject);
                for (String id : ids) {
                    BasePersons subjectByName = subjectService.getPersonBySubjectName(subject, Long.valueOf(id));
                    personList.add(subjectByName);
                }
            }
            persons.put(subjectTag, personList);
        }
        List<EventEntity> events = new ArrayList<>();
        if(subjectName.equals("重大行政处罚案件所涉人员")){
            events = penaltyPersonsService.selectEventsBySubjectId(subjectId);
        } else if (subjectName.equals("制假高危人群")) {
            events = fraudulentPersonsService.selectEventsBySubjectId(subjectId);
        } else if (subjectName.equals("历年刑事打击人员")) {
            events = criminalStrikePersonsService.selectEventsBySubjectId(subjectId);
        } else {
            throw new IllegalArgumentException();
        }

        HashMap<String, Set<Long>> itemMap = new HashMap<String, Set<Long>>();
        for (EventEntity event : events) {
            List<BaseSubjectExtend> subjects = eventEntityService.getSubjectsByEventId(event.getId());
            for (BaseSubjectExtend baseSubjectExtend : subjects) {
                if (baseSubjectExtend.getName().equals("车辆")) {
                    Set<Long> value = itemMap.getOrDefault("车辆", new HashSet<Long>());
                    value.add(baseSubjectExtend.getId());
                    itemMap.put("车辆", value);
                } else if (baseSubjectExtend.getName().equals("手机")) {
                    Set<Long> value = itemMap.getOrDefault("手机", new HashSet<Long>());
                    value.add(baseSubjectExtend.getId());
                    itemMap.put("手机", value);
                }
            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("events", events);
        jsonObject.put("items", itemMap);
        jsonObject.put("persons", persons);
        return R.ok().put("userprofile", jsonObject.toJSONString());
    }

    @GetMapping("/userprofile/{tags}")
    public R circleSelect(@RequestBody String[] tags) {
        HashMap<String, List<String>> users = labelService.circleSelect(tags);
        HashMap<String, BasePersons> stringMapHashMap = new HashMap<>();
        for (String subject : users.keySet()) {
            List<String> list = users.get(subject);
            for (String string : list) {
                String[] split = string.split(",");
                for (String s1 : split) {
                    long subjectId = Long.parseLong(s1);
                    BasePersons subjectByName = subjectService.getPersonBySubjectName(subject, subjectId);
                    stringMapHashMap.put(subject, subjectByName);
                }
            }
        }
        return R.ok().put("users", stringMapHashMap);
    }
}